package com.lhy.admin.common.secure.authentication;

import com.lhy.admin.Constants;
import com.lhy.admin.common.secure.SecureValidator;
import com.lhy.admin.util.AnnotationUtil;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;

public class AuthenticationValidator implements SecureValidator {

    @Override
    public boolean validate(HandlerMethod handlerMethod, HttpServletRequest request) {
        Authentication authentication = AnnotationUtil.getAnnotation(handlerMethod, Authentication.class);
        return authentication == null || validAuthentication(request);
    }


    private boolean validAuthentication(HttpServletRequest request) {
        Object user = request.getSession().getAttribute(Constants.SESSION_USER_KEY);
        return user != null;
    }
}
